/**
 * 
 */
package com.ideal.framework.pagetag.dao.impl;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ideal.framework.pagetag.DataList;
import com.ideal.framework.pagetag.dao.Entity;
import com.ideal.framework.pagetag.dao.IEntityDao;

/**
 * @author mengmingkuo
 * @create time 2008-7-15 13:44:19
 */
public abstract class EntityDaoImpl extends SqlMapClientDaoSupport implements IEntityDao{
	/**
	 * 可以查询单条数据,返回一个继承Entity类的JavaBean
	 */
	 public Object queryForObject(String method,Object entity)throws DataAccessException
	 {
	     return getSqlMapClientTemplate().queryForObject(method, entity);
	 }
	 /**
	  * 查询列表操作,String参数是调用ibatis的id,entity是继承此类的JAVABean
	  */
	public  List<Object> queryList(String method,Object entity) throws DataAccessException{
		return getSqlMapClientTemplate().queryForList(method, entity);
	}
	 /**
	  * 查询列表操作
	  */
	public  List<Object> queryList(String method) throws DataAccessException{
		return getSqlMapClientTemplate().queryForList(method);
	}
	/**
	 * 分页查询操作,entity是传递一个继承Entitye的JavaBean,
	 * pagenum是传递的每页显示条数,npage是传递的下一页参数,method是调用ibatis的id
	 */
	public DataList queryList(Entity entity, int pagenum, int npage, String method) throws DataAccessException
    {
	    if(npage < 1)
	        npage = 1;
	    if(pagenum < 0)
	        pagenum = 1;
	    int firstrow = pagenum * (npage - 1);
	    int lastrow = pagenum * npage + 1;
	    entity.setFirstrow(firstrow);
	    entity.setLastrow(lastrow);
	    List list = getSqlMapClientTemplate().queryForList(method, entity);
	    int total = getCount(entity, "count" + method).intValue();
	    DataList dataList = new DataList(list, total, pagenum, npage);
	    return dataList;
    }
	public Integer getCount(Entity entity, String method)throws DataAccessException
	{
		return (Integer)getSqlMapClientTemplate().queryForObject(method, entity);
	}
	/**
	 * 删除操作,String参数是调用ibatis的id,entity是继承此类的JAVABean
	 */
	public int delete(String method,Object entity)throws DataAccessException
    {
		return getSqlMapClientTemplate().delete(method, entity);
    }
	/**
	 * 更新操作,String参数是调用ibatis的id,entity是继承此类的JAVABean
	 */
	public int update(String method,Object entity)throws DataAccessException
	{
		//entity.setModifyTime(new Date());
		return getSqlMapClientTemplate().update(method, entity);
	}
	/**
	 * 新建操作,String参数是调用ibatis的id,entity是继承此类的JAVABean
	 */
	public void insert(String method,Object entity)throws DataAccessException
	{
	    //entity.setCreateTime(new Date());
	    getSqlMapClientTemplate().insert(method, entity);
	}
}
